/*
Copyright (c) 2010 julien barbay <barbay.julien@gmail.com>

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/

package martian.vo0doo.core 
{
	import flash.utils.Dictionary;
	
	import martian.vo0doo.core.Behavior;
	import martian.m4gic.tools.Domain;
	
	public class Abstract implements Behavior
	{
		private var _available:Boolean = false;
			public function get active():Boolean { return _available; }
		
		private var _name:String = "";
			public function get name():String { return _name; }
			public function set name(value:String):void { _name = value; }		
		
		private var _auto:Boolean = true;
			public function get auto():Boolean { return _auto; }
			public function set auto(value:Boolean):void { _auto = value; }
			
		private var _target:*;
			public function get target():* { return _target; }
			public function set target(value:*):void
			{
				if (auto)
				{
					if (value != null)
					{
						_target = value;
						activate();
					}
					else
					{
						desactivate();
						_target = value;
					}
				} else { _target = value; }
			}
		
		public function Abstract(args:* = null)
		{
			if (args != null) {	initialize(args); }
		}
		
		protected function initialize(args:*):void {}
		
		final public function activate():Behavior
		{
			if (!_available && _target != null)
			{
				_activate();
				_available = true;
			}
			
			return this;
		}
		
		protected function _activate():void {}
		
		final public function desactivate():Behavior
		{
			if (_available && _target != null)
			{
				_desactivate();
				_available = false;
			}
			
			return this;
		}
		
		protected function _desactivate():void {}
		
		final public function perform():Behavior { _perform(); return this; }
		
		protected function _perform():void {}
		
		public function clone():Behavior { return null; }
		
		public function dispose():void {}
	}
}